*******************************************************************************
* ANES.
* Jobs gaurantee and class.
* Separate models for each survey year.
*******************************************************************************

* Collect all frame names.
frames dir
local frn = r(frames)
local defn = "default"
local frn2: list frn- defn
* Drop frames that aren't default.
foreach fname of local frn2 {
	frame drop `fname'
}


*******************************************************************************
* Full sample.
*******************************************************************************

local varlst1 "pid7 polviews white age female south"

* Objective class.
* EGP 1.
frame copy default coef_jobs_egp1 , replace
frame coef_jobs_egp1 { 
	contract year if (jobs01 != . & egp5 != .)
	gen str var = "egp1"
	gen str dv = "Guarantee jobs/inc. (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	reg jobs01 egp5_1-egp5_4 `varlst1' if year==`yr' 
	matrix EGP = r(table)
	frame coef_jobs_egp1 {
		replace b = EGP[1,1] if year == `yr'
		replace ll = EGP[5,1] if year == `yr'
		replace ul = EGP[6,1] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}

* EGP 2.
frame copy default coef_jobs_egp2 , replace
frame coef_jobs_egp2 { 
	contract year if (jobs01 != . & egp5 != .)
	gen str var = "egp2"
	gen str dv = "Guarantee jobs/inc. (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	reg jobs01 egp5_1-egp5_4 `varlst1' if year==`yr'
	matrix EGP = r(table)
	frame coef_jobs_egp2 {
		replace b = EGP[1,2] if year == `yr'
		replace ll = EGP[5,2] if year == `yr'
		replace ul = EGP[6,2] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}


* All other class variables.
foreach v in subclass inc5 edu5 {
frame copy default coef_jobs_`v' , replace
frame coef_jobs_`v' { 
	contract year if jobs01 != . 
	gen str var = "`v'"
	gen str dv = "Guarantee jobs/inc. (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	capture reg jobs01 `v' `varlst1' if year==`yr'
	if _rc == 0 {
		matrix R = r(table)
		frame coef_jobs_`v' {
			replace b = R[1,1] if year == `yr'
			replace ll = R[5,1] if year == `yr'
			replace ul = R[6,1] if year == `yr'
			replace n = `e(N)' if year == `yr'
		}
	}
}
}


*******************************************************************************
* ANES.
* Culture and class.
* Separate models for each survey year.
*******************************************************************************

*******************************************************************************
* Full sample.
*******************************************************************************

local varlst1 "pid7 polviews white age female south"

* Objective class.
* EGP 1.
frame copy default coef_cul2_egp1 , replace
frame coef_cul2_egp1 { 
	contract year if (culpol01 != . & egp5 != .)
	gen str var = "egp1"
	gen str dv = "Culture policy (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	reg culpol01 egp5_1-egp5_4 `varlst1' if year==`yr' 
	matrix EGP = r(table)
	frame coef_cul2_egp1 {
		replace b = EGP[1,1] if year == `yr'
		replace ll = EGP[5,1] if year == `yr'
		replace ul = EGP[6,1] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}

* EGP 2.
frame copy default coef_cul2_egp2 , replace
frame coef_cul2_egp2 { 
	contract year if (culpol01 != . & egp5 != .)
	gen str var = "egp2"
	gen str dv = "Culture policy (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	reg culpol01 egp5_1-egp5_4 `varlst1' if year==`yr'
	matrix EGP = r(table)
	frame coef_cul2_egp2 {
		replace b = EGP[1,2] if year == `yr'
		replace ll = EGP[5,2] if year == `yr'
		replace ul = EGP[6,2] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}


* All other class variables.
foreach v in subclass inc5 edu5 {
frame copy default coef_cul2_`v' , replace
frame coef_cul2_`v' { 
	contract year if culpol01 != . 
	gen str var = "`v'"
	gen str dv = "Culture policy (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	capture reg culpol01 `v' `varlst1' if year==`yr'
	if _rc == 0 {
		matrix R = r(table)
		frame coef_cul2_`v' {
			replace b = R[1,1] if year == `yr'
			replace ll = R[5,1] if year == `yr'
			replace ul = R[6,1] if year == `yr'
			replace n = `e(N)' if year == `yr'
		}
	}
}
}


*******************************************************************************
* ANES.
* Race policy and class.
* Separate models for each survey year.
*******************************************************************************

*******************************************************************************
* White respondents only.
*******************************************************************************

local varlst2 "pid7 polviews age female south"

* Objective class.
* EGP 1.
frame copy default coef_rac2_egp1 , replace
frame coef_rac2_egp1 { 
	contract year if (racepol01 != . & egp5 != .)
	gen str var = "egp1"
	gen str dv = "Race policy (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	reg racepol01 egp5_1-egp5_4 `varlst2' if year==`yr' & white==1
	matrix EGP = r(table)
	frame coef_rac2_egp1 {
		replace b = EGP[1,1] if year == `yr'
		replace ll = EGP[5,1] if year == `yr'
		replace ul = EGP[6,1] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}

* EGP 2.
frame copy default coef_rac2_egp2 , replace
frame coef_rac2_egp2 { 
	contract year if (racepol01 != . & egp5 != .)
	gen str var = "egp2"
	gen str dv = "Race policy (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	reg racepol01 egp5_1-egp5_4 `varlst2' if year==`yr' & white==1
	matrix EGP = r(table)
	frame coef_rac2_egp2 {
		replace b = EGP[1,2] if year == `yr'
		replace ll = EGP[5,2] if year == `yr'
		replace ul = EGP[6,2] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}


* All other class variables.
foreach v in subclass inc5 edu5 {
frame copy default coef_rac2_`v' , replace
frame coef_rac2_`v' { 
	contract year if racepol01 != . 
	gen str var = "`v'"
	gen str dv = "Race policy (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	capture reg racepol01 `v' `varlst2' if year==`yr' & white==1
	if _rc == 0 {
		matrix R = r(table)
		frame coef_rac2_`v' {
			replace b = R[1,1] if year == `yr'
			replace ll = R[5,1] if year == `yr'
			replace ul = R[6,1] if year == `yr'
			replace n = `e(N)' if year == `yr'
		}
	}
}
}


*******************************************************************************
* ANES.
* Vote choice and class.
* Separate models for each survey year.
*******************************************************************************

*******************************************************************************
* Full sample.
*******************************************************************************


local varlst1 "pid7 polviews white age female south"

* Objective class.
* EGP 1.
frame copy default coef_vote2_egp1 , replace
frame coef_vote2_egp1 { 
	contract year if (demvote != . & egp5 != .)
	gen str var = "egp1"
	gen str dv = "Vote Dem. (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	logit demvote egp5_1-egp5_4 `varlst1' if year==`yr' 
	matrix EGP = r(table)
	frame coef_vote2_egp1 {
		replace b = EGP[1,1] if year == `yr'
		replace ll = EGP[5,1] if year == `yr'
		replace ul = EGP[6,1] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}

* EGP 2.
frame copy default coef_vote2_egp2 , replace
frame coef_vote2_egp2 { 
	contract year if (demvote != . & egp5 != .)
	gen str var = "egp2"
	gen str dv = "Vote Dem. (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	logit demvote egp5_1-egp5_4 `varlst1' if year==`yr'
	matrix EGP = r(table)
	frame coef_vote2_egp2 {
		replace b = EGP[1,2] if year == `yr'
		replace ll = EGP[5,2] if year == `yr'
		replace ul = EGP[6,2] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}


* All other class variables.
foreach v in subclass inc5 edu5 {
frame copy default coef_vote2_`v' , replace
frame coef_vote2_`v' { 
	contract year if (demvote != . & polviews != .)
	gen str var = "`v'"
	gen str dv = "Vote Dem. (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	capture logit demvote `v' `varlst1' if year==`yr'
	if _rc == 0 {
		matrix R = r(table)
		frame coef_vote2_`v' {
			replace b = R[1,1] if year == `yr'
			replace ll = R[5,1] if year == `yr'
			replace ul = R[6,1] if year == `yr'
			replace n = `e(N)' if year == `yr'
		}
	}
}
}


*******************************************************************************
* ANES.
* Vote choice and policy attitudes.
* Separate models for each survey year.
*******************************************************************************

*******************************************************************************
* Full sample.
*******************************************************************************


local varlst1 "pid7 polviews white age female south"

foreach v in jobs01 culpol01 racepol01 {
frame copy default coef_dvotepol2_`v' , replace
frame coef_dvotepol2_`v' { 
	contract year if (demvote != . & polviews != . & `v'!=.)
	gen str var = "`v'"
	gen str dv = "Vote Dem. (ANES)"
	gen b = .
	gen ll = .
	gen ul = .
	gen n = .
	levelsof year , local(lst_year)
}

foreach yr of numlist `lst_year' {
	logit demvote `v' `varlst1' if year==`yr'
	matrix R = r(table)
	frame coef_dvotepol2_`v' {
		replace b = R[1,1] if year == `yr'
		replace ll = R[5,1] if year == `yr'
		replace ul = R[6,1] if year == `yr'
		replace n = `e(N)' if year == `yr'
	}
}
}



* Combine frames.
* Collect all frame names.
frames dir
local frn = r(frames)
local defn = "default"
local frn2: list frn- defn

* New frame for append.
frame create sepmods_coef_anes

foreach fname of local frn2 {
	frame `fname' {
		tempfile `fname'
		save ``fname'', replace
	}
	frame sepmods_coef_anes : append using ``fname''
}

* Recode some labels.
frame sepmods_coef_anes {
	gen str model = "Model for each year"
	replace var = "Upper Class" if var == "egp1"
	replace var = "Upper Mid. Class" if var == "egp2"
	replace var = "Subjective Class" if var == "subclass"
	replace var = "Income" if var == "inc5"
	replace var = "Education" if var == "edu5"
	replace var = "Guarantee Jobs/Inc." if var == "jobs01"
	replace var = "Culture Policy" if var == "culpol01"
	replace var = "Race Policy" if var == "racepol01"
	save "results/coef_anes_sepmods.dta", replace
}

* Drop frames.
foreach fname of local frn2 {
	frame drop `fname'
}
frame drop sepmods_coef_anes


